﻿using Microsoft.EntityFrameworkCore;
using Reader.Models;

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }

    // DbSet 属性用于表示数据库中的表
    public DbSet<User> Users { get; set; }
    public DbSet<Book> Books { get; set; }
    
    public DbSet<ReadingRecord> ReadingRecords { get; set; }
    public DbSet<Chapter> Chapters { get; set; }
    
    public DbSet<BookCategory> Categories { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // 调用 User 类中的静态方法来配置实体
        User.Configure(modelBuilder.Entity<User>());
        Book.Configure(modelBuilder.Entity<Book>());
    }
    
    public User GetUserByUsernameAndPassword(string username, string password)
    {
        return Users.FirstOrDefault(u => u.Username == username && u.Password == password);
    }
    
    public void AddUser(User user)
    {
        Users.Add(user);
        SaveChanges();
    }
}